package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.annotation.AutoFill;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.exception.UpdationNotAllowedException;
import com.sky.mapper.DishFlavorMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

/**
 * @Author: WangHongKun
 * @Date: 2022/9/22 16:58
 * @Email: 2028911483@qq.com
 * @Phone: 18683977706
 */
@Slf4j
@Service
public class DishServiceImpl implements DishService {

    @Autowired
    private  DishMapper dishMapper;
    @Autowired
    private DishFlavorMapper dishFlavorMapper;
    @Autowired
    private SetmealMapper setmealMapper;

    @Autowired
    private SetmealDishMapper setmealDishMapper;

    @Override
    @Transactional
    public void addDish(DishDTO dishDTO) {
        Dish dish = new Dish();
        List<DishFlavor> flavors = dishDTO.getFlavors();
        BeanUtils.copyProperties(dishDTO,dish);
        dish.setStatus(StatusConstant.ENABLE);
        dishMapper.addDish(dish);
        Long id = dish.getId();
        if(flavors !=null && flavors.size()!=0){
            for(DishFlavor flavor:flavors) {
                flavor.setDishId(id);
            }
            dishFlavorMapper.addDishFlavor(flavors);
        }
        return;
    }

    @Override
    public PageResult<DishVO> pageQuery(DishPageQueryDTO dishPageQueryDTO) {
        PageHelper.startPage(dishPageQueryDTO.getPage(),dishPageQueryDTO.getPageSize());
        Page<DishVO> dishVOS = (Page<DishVO>) dishMapper.PageQuery(dishPageQueryDTO);
        long total =  dishVOS.getTotal();
        List<DishVO> dishVOList = dishVOS.getResult();
        return  new PageResult(total,dishVOList);
    }

    @Override
    @Transactional
    public void DeleteAll(String ids) {
        String[] strings = ids.split("，");
        List<Long> list = new ArrayList<>();
        for(String s: strings){
            list.add(Long.parseLong(s));
        }
        //判断菜品是否是起售  ，是则无法删除
        for(Long id : list){
            Dish dish = dishMapper.getById(id);
            if (dish == null || dish.getStatus().equals(StatusConstant.ENABLE)){
                throw  new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
            }
        }
        //判断菜品是否是套餐中的成员
        List<SetmealDish> setmealDishList = setmealDishMapper.getByIds(list);
        if( setmealDishList !=null && setmealDishList.size() !=0) {
            throw new DeletionNotAllowedException(MessageConstant.DISH_BE_RELATED_BY_SETMEAL);
        }

        dishMapper.DeleteAll(list);

        //删除的是dish --- 还有关联的flavors没删除

        dishFlavorMapper.DeleteBatch(list);
        return;
    }


    @Override
    public void startOrStop(Long id, Integer status){
        dishMapper.StartOrStop(id,status);
        return;
    }

    @Override
    @Transactional
    public void Update(DishDTO dishDTO) {
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO,dish);
        Long  id = dishDTO.getId();
        List<DishFlavor> flavors = dishDTO.getFlavors();
        dish.setStatus(StatusConstant.ENABLE);
        //判断是否是套餐内的菜品，若是套餐内的菜品不能直接修改
        SetmealDish setmealDish = setmealDishMapper.getBYId(id);
        if(setmealDish !=null )
        {
            throw  new UpdationNotAllowedException("该菜品在套餐内，无法修改");
        }
        //先删除原菜品的口味
        dishFlavorMapper.Delete(id);
        //进行修改重新设置菜品
        dishMapper.Update(dish);
        //菜品未设置口味, 则不重新设置菜品口味
        if(flavors ==null || flavors.size() ==0)
        {
            return;
        }
        for(DishFlavor dishFlavor:flavors)
        {
            dishFlavor.setDishId(id);
        }

        //重新设置菜品口味
        dishFlavorMapper.addDishFlavor(flavors);
        return;
    }

    @Override
    @Transactional
    public DishVO getById(Long id) {
        DishVO dishVO = dishMapper.getDTOById(id);
        dishVO.setId(id);
        List<DishFlavor> dishFlavors = dishFlavorMapper.getById(id);
        dishVO.setFlavors(dishFlavors);
        return dishVO;
    }

    @Override
    public List<Dish> getByCategoryId(Long categoryId) {
        List<Dish> dishList = dishMapper.getByCategoryId(categoryId);
        return dishList;
    }
    
    @Transactional
    @Override
    public List<DishVO> getVoByCategoryId(Long categoryId) {
        List<DishVO> dishVOList = dishMapper.getVoByCategoryId(categoryId);


        for (int i = 0; i < dishVOList.size(); i++) {
            DishVO dishVo  = dishVOList.get(i);
            //表示该菜品必须为起售
            if(dishVo.getStatus().equals(StatusConstant.DISABLE)){
                dishVOList.remove(i);
                i--;
                continue;
            }
            List<DishFlavor> flavorList = dishFlavorMapper.getByDishId(dishVo.getId());
            dishVo.setFlavors(flavorList);
        }
        return dishVOList;
    }


}
